package cn.xzz.pethome.emp.controller;

import cn.xzz.pethome.emp.domain.Employee;
import cn.xzz.pethome.emp.query.EmpQuery;
import cn.xzz.pethome.emp.service.IEmpService;
import cn.xzz.pethome.share.utils.AjaxResult;
import cn.xzz.pethome.share.utils.PageCount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//表示返回值是json数据的controller类
@RestController
@RequestMapping("/emp")
@Api(value = "员工的接口API文档",description = "员工的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmpService empService;

    //查询用GetMapping注解
    //@PathVariable参数要写这个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Employee Employee(@PathVariable("id") @ApiParam(name = "id属性", required = true) Long id) {
           return empService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> EmployeeList(){
        return empService.findempall();
    }



    //高级查询和新增
    @PostMapping
    @ApiOperation(value = "部门的高级查询")
    public PageCount<Employee> queryEmployee(@RequestBody EmpQuery query){
        System.out.println(empService.getPageCount(query));
        return  empService.getPageCount(query);
    }

    //新增和修改
    @PutMapping
    @ApiOperation(value = "新增或者修改,如果id为null就是新增反之修改")
    public AjaxResult Employee(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                empService.addemp(employee);
            }else {
                empService.updateemp(employee);
            }
            return AjaxResult.win();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.lose();
        }

    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult employee(@PathVariable("id") Long id){
        try {
            empService.delete(id);
            return AjaxResult.win();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.lose();
        }

    }

    @PatchMapping
    @ApiOperation(value = "员工的批量删除" )
    public AjaxResult dept(@RequestBody List<Long> ids){
        try {
            empService.batchdeleteemp(ids);
            return AjaxResult.win();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.lose();
        }
    }


}
